package com.muyan.common.kits;

import com.muyan.common.utils.ToolUtil;

import java.util.HashMap;
import java.util.Map;

public class MapKit {

    /**
     * map的key转为小写
     */
    public static Map<String, Object> toLowercaseKey(Map<String, Object> map) {
        Map<String, Object> tempMap = new HashMap<>();
        for (String key : map.keySet()) {
            tempMap.put(key.toLowerCase(), map.get(key));
        }
        return tempMap;
    }

    /**
     * 将map中的key由驼峰形式转换为下划线形式
     */
    public static Map<String, Object> toUnderlineKey(Map<String, Object> map) {
        Map<String, Object> res = new HashMap<>();
        if (ToolUtil.isEmpty(map)) return res;
        for (String key : map.keySet()) {
            res.put(StrKit.toUnderlineCase(key), map.get(key));
        }
        return res;
    }

    /**
     * 将map中的key由下划线形式转换为驼峰形式
     */
    public static Map<String, Object> toCameKey(Map<String, Object> map) {
        Map<String, Object> res = new HashMap<>();
        if (ToolUtil.isEmpty(map)) return res;
        for (String key : map.keySet()) {
            res.put(StrKit.toCamelCase(key), map.get(key));
        }
        return res;
    }

    /**
     * 快速构建参数
     */
    public static Map<String, Object> of(Object... value) {
        Map<String, Object> map = new HashMap<>();
        if (value == null || value.length % 2 != 0) {
            throw new IllegalArgumentException("参数必需成对传入");
        }
        for (int i = 0; i < value.length; i += 2) {
            map.put(value[i].toString(), value[i + 1]);
        }
        return map;
    }

    /**
     * 从map中删除多个键值<br/>
     *
     * @param srcMap 要操作的map
     * @param keys   要删除的key值逗号分隔 如：id,id1,id2
     */
    public static Map<String, Object> delEntry(Map<String, Object> srcMap, String keys) {
        if (srcMap != null && srcMap.size() > 0) {
            if (keys != null && !keys.isEmpty()) {
                for (String s : keys.split(",")) {
                    srcMap.remove(s);
                }
            }
        }
        return srcMap;
    }
}
